home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_08_03 / 8n03062a < prev    next >
Text File  |  1991-03-03  |  2KB  |  34 lines

  1. /*********** THREAD.H COPYRIGHT 1989 GREGORY COLVIN ************
  2. This program may be distributed free with this copyright notice.
  3. ***************************************************************/
  4. #include <setjmp.h>             /* defines jmp_buf            */
  5. #include <assert.h>             /* defines assert             */
  6. #include <stdio.h>              /* defines stderr for assert  */
  7. #include <stdlib.h>             /* defines abort for assert   */
  8.  
  9. typedef int Thread;             /* index into thread table    */
  10. typedef struct {
  11.     jmp_buf init;               /* state of thread for init   */
  12.     jmp_buf jump;               /* state of thread for jump   */
  13.     char *stack;                /* top of stack for thread    */
  14.     Thread free;                /* next thread on free list   */
  15. } *ThTabl;
  16. extern ThTabl Threads;          /* table of threads           */
  17. extern Thread ThCurr;           /* current thread             */
  18.  
  19. int    ThInit(int n,int size);  /* create n size byte threads */
  20. Thread ThNew(void(*f)(Thread)); /* fork and exec new thread   */
  21. Thread ThNext(void);            /* find next runnable thread  */
  22. Thread ThJump(Thread id);       /* jump to another thread     */
  23. void   ThExit(Thread id);       /* exit to another thread     */
  24. void   ThFree(void);            /* free the thread table      */
  25.  
  26. #define ThProbe()               /* don't blow your stack     */\
  27. { char p;                                                      \
  28.   assert(Threads[ThCurr].stack < &p);                          \
  29. }
  30. #define ThId()                  /* identify yourself         */\
  31.   ThCurr
  32. #define ThLive(id)              /* is this thread alive?     */\
  33.   !Threads[(id)].free
  34.